package com.ruoyi.mateproduction.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateInventory.domain.MateInventory;
import com.ruoyi.mateproduction.domain.MateChildren;
import com.ruoyi.mateproduction.domain.MateTransfer;
import com.ruoyi.mateproduction.domain.MateTransferInformation;
import com.ruoyi.mateproduction.domain.MateTransferModel;
import com.ruoyi.mateproduction.domain.MtCourseCheck;
import com.ruoyi.mateproduction.mapper.MateCourseMapper;
import com.ruoyi.mateproduction.mapper.MateTransferMapper;
import com.ruoyi.mateproduction.service.MateTransferService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateTransferServiceImpl implements MateTransferService {

    @Autowired
    MateTransferMapper mateTransferMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateCourseMapper mateCourseMapper;

    @Override
    public int addMateTransfer(MateTransfer mateTransfer) {

        int num = 0;
        Map<String, Object> resMap = new HashMap<>();
        try {
            num = mateTransferMapper.addMateTransfer(mateTransfer);
            if (mateTransfer.getMateTransferModels() != null) {
                for (MateTransferModel mateTransferModel : mateTransfer.getMateTransferModels()) {
                    mateTransferModel.setDeptId(SecurityUtils.getDeptId());
                    mateTransferModel.setTransferId(mateTransfer.getTransferId());
                    mateTransferMapper.addMateTransferModel(mateTransferModel);
                }

                MtAudit mtAudit = new MtAudit();
                mtAudit.setCompanyId(SecurityUtils.getDeptId());
                mtAudit.setOrderId(mateTransfer.getTransferId());
                mtAudit.setZjName(mateTransfer.getQualityName());
                mtAudit.setOrderType("11");
                mtAudit.setOrderNum(mateTransfer.getTransferNum());
                mtAudit.setAuditStatus("0");
                mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit);

            }

        } catch (Exception e) {
            resMap.put("orderId", mateTransfer.getTransferId());
            resMap.put("orderType", "11");
            mtAuditMapper.deleteMtAuditById(resMap);
            mateTransferMapper.removeMateTransfer(mateTransfer.getTransferId());
            mateTransferMapper.removeMateTransferModel(mateTransfer.getTransferId());
        }

        return num;
    }

    @Override
    public List<MateTransfer> mateTransferList(MateTransfer mateTransfer) {
        return mateTransferMapper.mateTransferList(mateTransfer);
    }

    @Override
    public MateTransfer selectMateTransfer(Long transferId) {
        MateTransfer mateTransfer = mateTransferMapper.selectMateTransfer(transferId);
        List<MateTransferInformation> mateTransferInformations = mateTransferMapper.selectMateTransferInformation(transferId);
        mateTransfer.setMateTransferInformations(mateTransferInformations);
        return mateTransfer;
    }

    @Override
    public void remove(Long transferId) {
        MateTransfer mateTransfer = mateTransferMapper.selectMateTransfer(transferId);
        for (MateTransferModel mateTransferModel : mateTransfer.getMateTransferModels()) {
            mateTransferMapper.removeChildren(mateTransferModel.getTransferModelId());
        }
        MtCourseCheck mtCourseCheck = new MtCourseCheck();
        mtCourseCheck.setMtCourseId(mateTransfer.getMtCourseId());
        mtCourseCheck.setCourseStatus("1");
        mateCourseMapper.updateCourse(mtCourseCheck);
        Map<String, Object> resMap = new HashMap<>();
        mateTransferMapper.removeMateTransfer(transferId);
        mateTransferMapper.removeMateTransferModel(transferId);
        resMap.put("orderId", transferId);
        resMap.put("orderType", "11");
        mtAuditMapper.deleteMtAuditById(resMap);
    }


    @Override
    public int updateMateTransfer(MateTransfer mateTransfer) {
        int num = 0;
        if (mateTransfer.getMateTransferModels() != null) {
            mateTransferMapper.removeMateTransferModel(mateTransfer.getTransferId());
            mateTransferMapper.removeChildren(mateTransfer.getTransferId());
            for (MateTransferModel mateTransferModel : mateTransfer.getMateTransferModels()) {
                mateTransferModel.setDeptId(SecurityUtils.getDeptId());
                mateTransferModel.setTransferId(mateTransfer.getTransferId());
                mateTransferMapper.addMateTransferModel(mateTransferModel);
                for (MateChildren mateChildren : mateTransferModel.getChildren()) {
                    mateChildren.setTransferModelId(mateTransferModel.getTransferModelId());
                    mateChildren.setTransferId(mateTransfer.getTransferId());
                    mateChildren.setDeptId(SecurityUtils.getDeptId());
                    mateTransferMapper.addChildren(mateChildren);
                }
            }
        }

        num = mateTransferMapper.updateMateTransfer(mateTransfer);
        return num;
    }


    @Override
    public List<MateTransfer> auditMateTransfer(Long deptId) {
        return mateTransferMapper.auditMateTransfer(deptId);
    }

    @Override
    public List<MateTransferModel> selectMateTransferModel(Long transferId) {
        return mateTransferMapper.selectMateTransferModel(transferId);
    }

    @Override
    public List<MateChildren> selectChildren(Long transferId) {
        return mateTransferMapper.selectChildren(transferId);
    }
}
